package com.hong.servicecmn.controller;

import com.hong.commonutil.result.Result;
import com.hong.model.model.cmn.Dict;
import com.hong.servicecmn.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(tags = {"数据字典"})
@RestController
@RequestMapping("/admin/cmn/dict")
public class DictController {
    @Autowired
    private DictService dictService;

    @ApiOperation("根据数据id查询子数据列表")
    @GetMapping("findChildData/{id}")
    public Result findChildData(@PathVariable Long id) {
        List<Dict> list = dictService.findChildData(id);
        return Result.ok(list);
    }

    @ApiOperation("导出数据字典接口")
    @GetMapping("exportData")
    public void exportDictData(HttpServletResponse httpServletResponse) {
        dictService.exportDictData(httpServletResponse);
    }

    @ApiOperation("导入数据字典")
    @PostMapping("importData")
    public Result importDictData(MultipartFile file) {
        System.out.println("test");
        dictService.importDictData(file);
        return Result.ok();
    }

    /**
     * 根据dictcode和value查询
     *
     * @param dictCode
     * @param value
     * @return
     */
    @ApiOperation("根据dictcode和value查询医院信息")
    @GetMapping("getName/{dictCode}/{value}")
    public String getName(@PathVariable String dictCode, @PathVariable String value) {
        String dictName = dictService.getDictName(dictCode, value);
        return dictName;
    }

    /**
     * 根据value查询
     * @param value
     * @return
     */
    @ApiOperation("根据value查询医院信息")
    @GetMapping("getName/{value}")
    public String getName(@PathVariable String value) {
        String dictName = dictService.getDictName(null, value);
        return dictName;
    }

    @ApiOperation("根据dictCode查询下级节点")
    @GetMapping("findByDictCode/{dictCode}")
    public Result findByDictCode(@PathVariable String dictCode) {
        List<Dict> dicts = dictService.findByDictCode(dictCode);
        return Result.ok(dicts);
    }
}
